<?php
/**
 * User: lsx
 * Date: 2017/5/23
 * Time: 14:44
 * Progress:0%
 */

namespace logic\wechat\delivery;


use model\Account;
use model\ShopWatercardOrder;
use think\Config;
use think\Cookie;

class LUser
{
    private $aid;
    private $account;
    private $shopWatercardOrder;
    public function __construct()
    {
        $this->account=new Account();
        $this->shopWatercardOrder=new ShopWatercardOrder();
        $this->aid = Cookie::get(Config::get('const.cookie_name_wechat'));
    }

    public function logic_index(){
        $account=$this->account->where(['id'=>$this->aid])->find();
        $total_order=$this->shopWatercardOrder->where(['account_id'=>$this->aid,'flag_confirm'=>1])->count();
        $total_prepare=$this->shopWatercardOrder->where(['account_id'=>$this->aid,'order_status'=>['in',['110','210']]])->count();
        $total_finish=$this->shopWatercardOrder->where(['account_id'=>$this->aid,'order_status'=>['in',['111','211']]])->count();
        $data=array(
            'delivery_name'=>$account->name,
            'delivery_headimg'=>$account->headimg,
            'total_order'=>$total_order,
            'today_prepare'=>$total_prepare,
            'today_finish'=>$total_finish
        );
        return $data;
    }

    public function logic_my_delivery_list($param){
        $where=array(
            'delivery_id'=>$this->aid,
        );
        switch ($param['status']){
            case '0':
                $where['order_status']=['in',['110','210']];
                break;
            case '1':
                $where['order_status']=['in',['111','211']];
                break;
        }
        $total=$this->shopWatercardOrder->where($where)->count();
        $data=$this->shopWatercardOrder->alias('s')
            ->join('shop_user_watercard w','w.id=s.user_card_id','left')
            ->join('shop_watercard c','c.id=w.card_id','left')
            ->field("s.*,c.name")
            ->where($where)->limit($param['offset'],$param['limit'])->order('flag_confirm asc,order_time desc')->select();
        return array(
            'total'=>$total,
            'rows'=>$data
        );
    }

    public function logic_my_delivery_detail($id){
        return $this->shopWatercardOrder->alias('s')
            ->join('shop_user_watercard w','w.id=s.user_card_id','left')
            ->join('shop_watercard c','w.card_id=c.id','left')
            ->join('account a','s.delivery_id=a.id','left')
            ->join('shop_goods g','c.goods_id=g.id','left')
            ->join('shop_store t','s.store_id=t.id','left')
            ->field("s.*,a.name as delivery_name,a.mobile as delivery_mobile,g.name as goods_name,t.name as store_name,(case 
                when (order_status = 110 or order_status = 210 ) then '待配送'
                when (order_status = 111 or order_status = 211 ) then '已配送'
                else order_status end)
                as 'order_status'")
            ->where(['s.id'=>$id])
            ->find();
    }

    public function logic_my_delivery_order_confirm($id){
        $param=array(
            'order_status'=>111,
            'flag_confirm'=>1,
            'confirm_time'=>time()
        );
        $res= $this->shopWatercardOrder->save($param,['id'=>$id]);
        if($res){
            return array(
                'success'=>true,
                'message'=>'操作成功'
            );
        }else{
            return array(
                'success'=>false,
                'message'=>'操作失败'
            );
        }
    }

}